package com.xy.Servlet;

import java.io.IOException;
import java.util.Date;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.xy.dao.impl.PageResults;
import com.xy.model.Category;
import com.xy.model.Major;
import com.xy.service.CategoryService;
import com.xy.service.impl.CategoryServiceImpl;

/**
 * Servlet implementation class CategoryServlet
 */
@WebServlet("/categoryServlet")
public class CategoryServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public CategoryServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	  CategoryService service = new CategoryServiceImpl();
	  
	  if ("add".equals(request.getParameter("status"))) {
	    Category category = new Category();
	    category.setName(request.getParameter("name"));
	    category.setRemark(request.getParameter("remark"));
	    category.setCreateDate(new Date());
        if(service.add(category)) {
          request.getRequestDispatcher("/categoryServlet?status=list&currentPage=0").forward(request, response);
        }else {
          System.out.println("error");
         request.getRequestDispatcher("/pages/student/student002/add_type.jsp").forward(request, response);
        }
	    
      } else if ("list".equals(request.getParameter("status"))) {
        int currentPage = 0;
        if (request.getParameter("goalpage") == null) {
          currentPage = Integer.valueOf(request.getParameter("currentPage")) - 1;
        } else
          currentPage = Integer.valueOf(request.getParameter("goalpage")) - 1;

        PageResults pageResults = service.query("%",currentPage);

        request.setAttribute("pageResults", pageResults);
        request.setAttribute("status", "list");

        request.getRequestDispatcher("/pages/student/student002/query_result.jsp").forward(request, response);
        
      } else if ("query".equals(request.getParameter("status"))) {
        int currentPage = 0;
        if (request.getParameter("goalpage") == null) {
          currentPage = Integer.valueOf(request.getParameter("currentPage")) - 1;
        } else
          currentPage = Integer.valueOf(request.getParameter("goalpage")) - 1;
        String list = null;
        String name = null;
        if (request.getParameter("list") == null) {
          name = request.getParameter("name") == null ? "%" : request.getParameter("name");
          list = name;
        } else {
          list = new String(request.getParameter("list").getBytes("iso-8859-1"), "utf-8");
          name = list;
        }
        PageResults pageResults = service.query(name,currentPage);
        request.setAttribute("pageResults", pageResults);
        request.setAttribute("status", "query");
        request.setAttribute("list", list);
        
        request.getRequestDispatcher("/pages/student/student002/query_result.jsp").forward(request, response);
        
      } else if ("del".equals(request.getParameter("status"))) {
        int id = Integer.valueOf(request.getParameter("id"));
        Category category = (Category) service.get(id);
        if(service.delete(category)) {
          request.getRequestDispatcher("/categoryServlet?status=list&currentPage=0").forward(request,
              response);
        }else {
          System.out.println("error");
          request.getRequestDispatcher("/categoryServlet?status=list&currentPage=0").forward(request,
              response);
        }
        
      }else if("goup".equals(request.getParameter("status"))) {
        int id = Integer.valueOf(request.getParameter("id"));
        Category category = (Category) service.get(id);
        request.setAttribute("id", id);
        request.setAttribute("category", category);
        request.getRequestDispatcher("/pages/student/student002/modify_type.jsp").forward(request,
            response);
        
      }else if ("up".equals(request.getParameter("status"))) {
        int id = Integer.valueOf(request.getParameter("id"));
        Category category = (Category) service.get(id);
        category.setName(request.getParameter("name"));
        category.setRemark(request.getParameter("remark"));
        if(service.update(category)) {
          request.getRequestDispatcher("/categoryServlet?status=list&currentPage=0").forward(request,
              response);
        }else {
          System.out.println("error");
          request.getRequestDispatcher("/categoryServlet?status=list&currentPage=0").forward(request,
              response);
        }
      }
	}

}
